﻿using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Text;

namespace LDBUtilities
{
    // An instance of the SortWrapper class is created for
    // each item and added to the ArrayList for sorting.
    public class ListViewSortWrapper
    {
        private int sortColumn;
        private ListViewItem sortItem;
        public ListViewItem SortItem
        {
            get { return sortItem; }
        }


        // A SortWrapper requires the item and the index of the clicked column.
        public ListViewSortWrapper(ListViewItem Item, int iColumn)
        {
            sortItem = Item;
            sortColumn = iColumn;
        }

        // Text property for getting the text of an item.
        public string Text
        {
            get
            {
                return sortItem.SubItems[sortColumn].Text;
            }
        }

        // Implementation of the IComparer
        // interface for sorting ArrayList items.
        public class SortComparer : IComparer<ListViewSortWrapper>
        {
            bool ascending;

            // Constructor requires the sort order;
            // true if ascending, otherwise descending.
            public SortComparer(bool asc)
            {
                this.ascending = asc;
            }

            // Implemnentation of the IComparer:Compare
            // method for comparing two objects.
            public int Compare(ListViewSortWrapper xItem, ListViewSortWrapper yItem)
            {
                string xText = xItem.SortItem.SubItems[xItem.sortColumn].Text;
                string yText = yItem.SortItem.SubItems[yItem.sortColumn].Text;
                return xText.CompareTo(yText) * (this.ascending ? 1 : -1);
            }
        }
    }
}
